<?php
/**
 * Mã hóa MD5
 *
 */
 function utf16_to_utf8($str) {
    $c0 = ord($str[0]);
    $c1 = ord($str[1]);

    if ($c0 == 0xFE && $c1 == 0xFF) {
        $be = true;
    } else if ($c0 == 0xFF && $c1 == 0xFE) {
        $be = false;
    } else {
        return $str;
    }

    $str = substr($str, 2);
    $len = strlen($str);
    $dec = '';
    for ($i = 0; $i < $len; $i += 2) {
        $c = ($be) ? ord($str[$i]) << 8 | ord($str[$i + 1]) : 
                @ord($str[$i + 1]) << 8 | @ord($str[$i]);
        if ($c >= 0x0001 && $c <= 0x007F) {
            $dec .= chr($c);
        } else if ($c > 0x07FF) {
            $dec .= chr(0xE0 | (($c >> 12) & 0x0F));
            $dec .= chr(0x80 | (($c >>  6) & 0x3F));
            $dec .= chr(0x80 | (($c >>  0) & 0x3F));
        } else {
            $dec .= chr(0xC0 | (($c >>  6) & 0x1F));
            $dec .= chr(0x80 | (($c >>  0) & 0x3F));
        }
    }
    return $dec;
	}
function pass_md5( $pass ) {   	
	$value = md5( md5( 'sbr'.$pass ) );    	
	return $value;
}

function remove_string ( $string ) {
	$string = str_replace(array("*", "'", '"', ";", "=","<", ">", "[", "]"), "", $string); 
	return $string; 
}

function check_login() {
	$CI = & get_instance();
	$lang =$CI->session->userdata('lang');
	
	if($lang == "")
	{
		$data_sess=array("lang" =>"vi");

		$CI->session->set_userdata($data_sess);
		$la = "vietnamese";
	}
	else
	{	
	
		$la = $CI->session->userdata("lang");
		if($la == "en")
			$la = "english";
		else if($la == "cn")
				$la = "chinese";
			else
				$la = "vietnamese";
	}
	$CI->lang->load('admin', $la);
	$name = $CI->session->userdata('admin_username');
	$level = (int)$CI->session->userdata('level');
	
	if ( (!$name && !$level)  ) {
			redirect("admin/login","refresh");
	}
	else
	
	{
		//echo $level;
	}
}

function check_login_comp() {
	$CI = & get_instance();

	$name = $CI->session->userdata('email');
	//$level = (int)$CI->session->userdata('level');
	
	if ( (!$name ) ) {
		
		
	/*	echo "<script>alert('Bạn phải đang nhập để có thể thành toán'); var login = document.getElementById('username');
login.focus();  </script>";
	/*	$referer = $_SERVER['HTTP_REFERER'];
   if (!$referer == '') {
      echo '<p><a href="' . $referer . '" title="Return to the previous page">&laquo; Go back</a></p>';
   } else {
      echo '<p><a href="javascript:history.go(-1)" title="Return to the previous page">&laquo; Go back</a></p>';
   }*/
		return 0;
		
	}
	else
	{
			return 1;
	}
}


function get_username() {
	$CI = & get_instance();
		
	$name = $CI->session->userdata('name');
	
	return $name;
}
function get_level() {
	$CI = & get_instance();
		
	$level = $CI->session->userdata('level');
	
	return $level ;
}
function get_userid() {
	$CI = & get_instance();
		
	$user_id = $CI->session->userdata('sbradio');
	
	return $user_id;
}
function check_user()
{
	$CI = & get_instance();
		$name = $CI->session->userdata('email');
	$level = (int)$CI->session->userdata('level');
	
	if ( (!$name && !$level)) {
		
		redirect("login","refresh");
	}
	else
	
	{
		//echo $level;
	}
}

function load_lang($language)
{	
	$CI = & get_instance();
	$lang =$CI->session->userdata('lang');
	//echo $lang;
	//echo $CI->session->userdata("lang");
	$la = "vietnamese";
	if($lang == "")
	{
		$data_sess=array("lang" =>"vi");

		$CI->session->set_userdata($data_sess);
//		$la = "vietnamese";

	}
	else
	{	
	
		$la = $CI->session->userdata("lang");
		if($la == "en")
			$la = "english";
		else if($la == "cn")
				$la = "chinese";
			else
				$la = "vietnamese";
	}
	//echo $la;
	$CI->lang->load($language, $la);
}

function create_thumb($images)
{
	$CI = & get_instance();
	$upimg['image_library'] = 'gd2';
	$upimg['source_image']	= './upload/'.$images;
	$upimg['create_thumb'] = TRUE;
$upimg['maintain_ratio'] = TRUE;
$upimg['width'] = 150;
$upimg['height'] = 150;
$CI->load->library('image_lib', $upimg);
if( $CI->image_lib->resize()){
echo $images;}
else
{
	echo $images;
	 echo $CI->image_lib->display_errors();
	echo "<br /> ko thanh cong";
	echo "<pre>";
	print_r($upimg);
	echo "</pre>"; 
	
}
	
	}

function image_thumb($imgname, $module, $height="150", $width="150") {

	$CI =& get_instance();



	$image_path = 'upload/thumb/';

	

	$image_thumb = $image_path . '/' . $height . '_' . $width . '_' . $imgname;



	if( ! file_exists($image_thumb)){

		// LOAD LIBRARY

		$CI->load->library('image_lib');



		// CONFIGURE IMAGE LIBRARY

		$config['image_library']	= 'gd2';

		$config['source_image']		= "upload/".$module."/".$imgname;

		$config['new_image']		= $image_thumb;

		$config['maintain_ratio']	= TRUE;

		$config['height']			= $height;

		$config['width']			= $width;

		$CI->image_lib->initialize($config);

		$CI->image_lib->resize();

		$CI->image_lib->clear();

	}



	return '<img src="' . base_url() .  $image_thumb . '" alt="" />';

}

function load_module($data)
{	
	
	$CI = & get_instance();
	$CI->load->model("Mpermission");
//	$module = $CI->db->get("permission")->array_result();
	$module = $CI->Mpermission->get_module();
	//print_r($module);
	
	foreach ($module as $mod )
	{
		$array_mod[] =$mod["name"] ;
		
	}
	
	
	if($data != "fullcontrol")
	{

			foreach ($data as $item)
			{
					if(in_array($item,$array_mod))
					{
							

							switch($item)
							{
								case "permission":
											echo menu_url('admin/permission/', 'Quán lý Quyền hạn', false);
									
								break;
								case "user":
									echo menu_url('admin/user/', 'Ban Quản trị', false);
									echo menu_url('admin/customer/', 'Thành viên', false);
									echo menu_url('admin/card/', 'Thẻ Thành viên', false);
								break;
							
								case "cate":
									echo menu_url('admin/cate/', 'Ngành Hàng', false);
					
									echo menu_url('admin/sub_cate/', 'Nhóm Hàng', false);
									
									break;
								case "product":
									echo menu_url('admin/unit/', 'Đơn Vi Sản phẩm', false);
									echo menu_url('admin/product/', 'Sản Phẩm', false);
									
									break;
								case "news":
									 //Loại tin tức$CI->lang->line('menu_news_cate')
				
							  echo menu_url('admin/news/cate',"Loại tin tức", false);
				
								  // Tin tức$CI->lang->line('menu_news')$CI->lang->line('menu_ads')
				
							  echo menu_url('admin/news/',"Tin tức", false);
								echo menu_url('admin/pages/', 'Các thông tin website', false);
								 echo menu_url('admin/advertisement/',"Quảng cáo", false);
								break;
								case "cart":
									 echo '<li class="havesub"><a href="#">Đơn đặt hàng</a>'; //QUẢN LÝ NHÂN SỰ
				
									echo '<ul>';
								
									
								
								  echo menu_url('admin/order/index/undelivered', 'Đơn Chưa Giao', false);
								echo menu_url('admin/order/index/delivered', 'Đơn Đã Giao', false);
						  
								echo menu_url('admin/order/index/deleted', 'Đơn Đã Xóa', false);
									echo '</ul></li>'; 
									break;
					
							}
					
					}
				}
		}
		else if($data == "fullcontrol")
			{
				echo menu_url('admin/permission/', 'Quán lý Quyền hạn', false);
				echo menu_url('admin/user/', 'Ban Quản trị', false);
				echo menu_url('admin/customer/', 'Thành viên', false);
				echo menu_url('admin/card/', 'Thẻ Thành viên', false);
				echo menu_url('admin/cate/', 'Ngành Hàng', false);
				
				echo menu_url('admin/sub_cate/', 'Nhóm Hàng', false);
				echo menu_url('admin/unit/', 'Đơn Vi Sản phẩm', false);
				echo menu_url('admin/product/', 'Sản Phẩm', false);
		//		echo menu_url('admin/promotion/', 'Khuyến Mãi', false);
					
				 //Loại tin tức
			
			  	  echo menu_url('admin/news/cate',"Loại tin tức", false);
			
			  	// Tin tức
			
			  			  echo menu_url('admin/news/',"Tin tức", false);

	
		echo menu_url('admin/pages/', 'Các thông tin website', false);
					 echo menu_url('admin/advertisement/',"Quảng cáo", false);
			
				echo '<li class="havesub"><a href="#">Đơn đặt hàng</a>'; 
			
				echo '<ul>';
			
			    echo menu_url('admin/order/index/undelivered', 'Đơn Chưa Giao', false);
				echo menu_url('admin/order/index/delivered', 'Đơn Đã Giao', false);
				echo menu_url('admin/order/index/deleted', 'Đơn Đã Xóa', false);
  
		
			echo '</ul></li>'; 
			}
			
			
	
	/*    echo menu_url('admin/permission/', 'Quán lý Quyền hạn', false);
    echo menu_url('admin/user/', 'Ban Quản trị', false);
    echo menu_url('admin/customer/', 'Thành viên', false);
	
	echo menu_url('admin/cate/', 'Ngành Hàng', false);
	
	echo menu_url('admin/sub_cate/', 'Nhóm Hàng', false);
	
	echo menu_url('admin/product/', 'Sản Phẩm', false);
		echo menu_url('admin/promotion/', 'Khuyến Mãi', false);
		
	 //Loại tin tức

  echo menu_url('admin/news/cate',$this->lang->line('menu_news_cate'), false);

  // Tin tức

  echo menu_url('admin/news/',$this->lang->line('menu_news'), false);

	
		
	
	 echo '<li class="havesub"><a href="#">Đơn đặt hàng</a>'; //QUẢN LÝ NHÂN SỰ

	echo '<ul>';

	echo menu_url('admin/order/1', 'Đơn Đã Giao', false);

  echo menu_url('admin/order/2', 'Đơn Chưa Giao', false);

  

	echo '</ul></li>'; */
}
function unicodeConvert($string) { //neu flag=1 thi thay the khoang trang

		if(!$string) return false;

		$unicode = array(

			'a'=>array('á','à','ả','ã','ạ','ă','ắ','ặ','ằ','ẳ','ẵ','â','ấ','ầ','ẩ','ẫ','ậ'),

			'A'=>array('Á','À','Ả','Ã','Ạ','Ă','Ắ','Ặ','Ằ','Ẳ','Ẵ','Â','Ấ','Ầ','Ẩ','Ẫ','Ậ'),

			'd'=>array('đ'),

			'D'=>array('Đ'),

			'e'=>array('é','è','ẻ','ẽ','ẹ','ê','ế','ề','ể','ễ','ệ'),

			'E'=>array('É','È','Ẻ','Ẽ','Ẹ','Ê','Ế','Ề','Ể','Ễ','Ệ'),

			'i'=>array('í','ì','ỉ','ĩ','ị'),

			'I'=>array('Í','Ì','Ỉ','Ĩ','Ị'),

			'o'=>array('ó','ò','ỏ','õ','ọ','ô','ố','ồ','ổ','ỗ','ộ','ơ','ớ','ờ','ở','ỡ','ợ'),

			'O'=>array('Ó','Ò','Ỏ','Õ','Ọ','Ô','Ố','Ồ','Ổ','Ỗ','Ộ','Ơ','Ớ','Ờ','Ở','Ỡ','Ợ'),

			'u'=>array('ú','ù','ủ','ũ','ụ','ư','ứ','ừ','ử','ữ','ự'),

			'U'=>array('Ú','Ù','Ủ','Ũ','Ụ','Ư','Ứ','Ừ','Ử','Ữ','Ự'),

			'y'=>array('ý','ỳ','ỷ','ỹ','ỵ'),

			'Y'=>array('Ý','Ỳ','Ỷ','Ỹ','Ỵ')

		);

	//	if ($flag==1)	$unicode['_'] = array(' ','&quot;','.');

		foreach($unicode as $nonUnicode=>$uni) {

			foreach($uni as $value)

			$string = str_replace($value, $nonUnicode, $string);

		}

		return $string;

	}
?>